package com.wkq.util


import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.widget.Toast

/**
 * 国外平台分享
 */
object ShareIntentUtil {

    // 支持单一包名分享（兼容旧代码）
    fun shareToSpecificApp(
        context: Context,
        packageName: String,
        contentUri: Uri,
        mimeType: String,
        notInstalledMessage: String
    ) {
        shareToSpecificApp(
            context,
            listOf(packageName),
            contentUri,
            mimeType,
            notInstalledMessage
        )
    }

    // 支持多个包名：命中任意一个即分享
    fun shareToSpecificApp(
        context: Context,
        packageNames: List<String>,
        contentUri: Uri,
        mimeType: String,
        notInstalledMessage: String
    ) {
        val pm = context.packageManager
        val targetPackage = packageNames.firstOrNull { pkg ->
            try {
                pm.getPackageInfo(pkg, 0)
                true
            } catch (e: PackageManager.NameNotFoundException) {
                false
            }
        }

        if (targetPackage != null) {
            val intent = Intent(Intent.ACTION_SEND).apply {
                type = mimeType
                putExtra(Intent.EXTRA_STREAM, contentUri)
                setPackage(targetPackage)
                addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            }

            try {
                context.startActivity(intent)
            } catch (e: ActivityNotFoundException) {
                Toast.makeText(context, notInstalledMessage, Toast.LENGTH_SHORT).show()
            }
        } else {
            Toast.makeText(context, notInstalledMessage, Toast.LENGTH_SHORT).show()
        }
    }

    // 分享视频到 Facebook
    fun shareVideoToFacebook(context: Context, videoUri: Uri) {
        val fbPackages = listOf("com.facebook.katana", "com.facebook.lite")
        shareToSpecificApp(context, fbPackages, videoUri, "video/*", "Please install Facebook")
    }

    // 分享视频到 Twitter
    fun shareVideoToTwitter(context: Context, videoUri: Uri) {
        val twitterPackages = listOf("com.twitter.android", "com.twitter.android.lite")
        shareToSpecificApp(context, twitterPackages, videoUri, "video/*", "Please install Twitter")
    }

    // 分享视频到 Telegram
    fun shareVideoToTelegram(context: Context, videoUri: Uri) {
        val telegramPackages = listOf("org.telegram.messenger", "org.telegram.messenger.web")
        shareToSpecificApp(context, telegramPackages, videoUri, "video/*", "Greetings on Telegram")
    }

    // 示例：分享到 Facebook（支持正常版和 Lite）
    fun shareImageToFacebook(context: Context, imageUri: Uri) {
        val fbPackages = listOf("com.facebook.katana", "com.facebook.lite")
        shareToSpecificApp(context, fbPackages, imageUri, "image/*", "Please install Facebook")
    }

    // 示例：分享到 Twitter（支持正常版和 X）
    fun shareImageToTwitter(context: Context, imageUri: Uri) {
        val twitterPackages = listOf("com.twitter.android", "com.twitter.android.lite")
        shareToSpecificApp(context, twitterPackages, imageUri, "image/*", "Please install Twitter")
    }

    // 示例：分享到 Telegram
    fun shareImageToTelegram(context: Context, imageUri: Uri) {
        val telegramPackages = listOf("org.telegram.messenger", "org.telegram.messenger.web")
        shareToSpecificApp(context, telegramPackages, imageUri, "image/*", "Greetings on Telegram")
    }

    // 示例：分享到 WhatsApp（正常+Business）
    fun shareImageToWhatsApp(context: Context, imageUri: Uri) {
        val waPackages = listOf("com.whatsapp", "com.whatsapp.w4b")
        shareToSpecificApp(context, waPackages, imageUri, "image/*", "Please greet WhatsApp")
    }
}
